package cate.game.world.invite;

import cate.common.game.invite.CateInviteEvent;
import cate.common.game.invite.InviteDef;
import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cate.game.role.Role;
import cate.game.role.world.invite.*;
import cate.game.role.world.invite.comm.Invitee;
import cate.game.role.world.invite.msg.InviteTaskUpdateResp;
import cp.solution.business.world.invite.dto.InviteEvent;
import cp.solution.define.LogcatTag;
import cp.solution.rpc.path.RpcSharePath;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InviteManager extends ModuleMan {

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
    }

    @Override
    public void initDependent() throws Exception {
        game.rpcRecv().onFireAndForgetTemplate(RpcSharePath.INVITE_EVENT_W2G, CateInviteEvent.class, (h, input) -> this.handleEvent(input));
    }

    public void tick() {

    }

    public void handleEvent(CateInviteEvent event) {
        game.logcat().log(LogcatTag.INVITE, "游戏服收到world通知的invite事件="+event);
        Role inviter = game.role.getRole(event.inviterId);
        if(inviter == null) {
            game.logcat().log(LogcatTag.INVITE, "但是在本服没有找到 角色(id="+ event.inviterId+")");
            return;
        }
        switch (event.type) {
            case InviteEvent.CREATE:
                handleCreate(inviter, event);
                break;
            case InviteEvent.LOGIN:
                handleLogin(inviter, event);
                break;
            case InviteEvent.LEVEL_UP:
                handleLevelUp(inviter, event);
                break;
            default:
                log.info("未知invite事件");
                break;
        }
    }

    private void handleCreate(Role inviter, CateInviteEvent event) {
        game.logcat().log(LogcatTag.INVITE, "有玩家点了你的邀请之后，创角了");
        InviteCreateTask task = inviter.getBase().invite.createTask;
        if(!inviter.getBase().invite.existInvitee(event.inviteeId) && task.doneNum < InviteDef.getCreateTaskNumMax()) {
            task.doneNum++;
            InviteTaskUpdateResp.build(task).sendNow(inviter);
            // 添加 受邀人数据
            inviter.getBase().invite.addInvitee(Invitee.fromEvent(event));
        }
    }

    private void handleLogin(Role inviter, CateInviteEvent event) {
        game.logcat().log(LogcatTag.INVITE, "有玩家点了你的邀请之后，登录了");
        InviteLoginTask task = inviter.getBase().invite.loginTask;
        if(task.doneNum < InviteDef.getLoginTaskNumMax() && !task.inviteeIds.contains(event.inviteeId)) {
            task.doneNum++;
            task.inviteeIds.add(event.inviteeId);
            InviteTaskUpdateResp.build(task).sendNow(inviter);
        }
    }

    private synchronized void handleLevelUp(Role inviter, CateInviteEvent event) {
        game.logcat().log(LogcatTag.INVITE, "有玩家点了你的邀请之后，升级了");
        RoleInvite ri = inviter.getBase().invite;
        Invitee invitee = ri.getInvitee(event.inviteeId);
        if(invitee == null) {
            log.warn("居然没有找到受邀人，但是收到了受邀人事件 inviter={}, event={}", inviter, event);
            return;
        }
        invitee.readEventBase(event);
        ri.noticeInviteeChanged();
    }
}
